package site.assad.service;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import site.assad.domain.User;

import javax.cache.annotation.CacheKey;
import javax.cache.annotation.CacheRemove;
import javax.cache.annotation.CacheResult;

/**
 * Author: Al-assad 余林颖
 * E-mail: yulinying_1994@outlook.com
 * Date: 2017/12/15 11:37
 * Description: 使用基于aop/tx 进行 xml 事务增强配置
 */
@Service
public class UserService {

    private final static Logger log = LogManager.getLogger();

    //获取元素
    @Cacheable(cacheNames = "users", key="#userId")
    public User getUser(@CacheKey int userId) {
        log.debug("real query user from DB");
        //模拟从数据库中获取 User 对象
        User user = new User();
        user.setId(userId);
        user.setName("assad");
        user.setPassword("233333");
        user.setIcon("icon-001");
        return user;
    }

    //将 userId 对应的 User 从缓存中删除
    @CacheEvict(cacheNames = "users", key="#userId")
    public void removeUserFromCache(@CacheKey int userId){
        log.debug("remove user "+ userId + " from cache");
    }



}
